#include "Buzzer.h"

//带增压电感驱动的无源蜂鸣器
uint8_t Beep_Enable;


void Buzzer_Init(void)
{
    GPIO_Init(GPIOB0,IO_Buzzer,Output); 
    GPIO_Buzzer_Close();
}

//Tick 2ms
void Buzzer_Clock_2ms(void)
{
    static uint16_t beep_Count = 0;
    static uint16_t beep_Alarm_L = 0;
    static uint16_t beep_Alarm_Count = 0;
    static uint16_t beep_Cycle = 0;
    static uint16_t beep_Cycle_Count;

    if(Beep_Enable == 3)
    { //Normal key sounds
        GPIO_Buzzer_Open();
        beep_Count++;
        if(beep_Count > 1)
        {
            GPIO_Buzzer_Close();
            Beep_Enable = 0;
            beep_Count = 0;
        }
    }
    else if(Beep_Enable == 4)
    { //Alarm
        if(beep_Cycle_Count < 4)
        {
            if(beep_Cycle < 4)
            {
                if(beep_Cycle == 3) beep_Alarm_L = 50;
                else beep_Alarm_L = 100;
                if(beep_Alarm_Count < 10)
                {
                    GPIO_Buzzer_Open();
                    beep_Alarm_Count++;
                }
                else if(beep_Alarm_Count < (10 + beep_Alarm_L))
                {
                    GPIO_Buzzer_Close();
                    beep_Alarm_Count++;
                }
                else
                {
                    beep_Alarm_Count = 0;
                    beep_Cycle++;
                }
            }
            else if(beep_Cycle < (4 + 500))
            {
                beep_Cycle++;
            }
            else
            {
                beep_Cycle = 0;
                beep_Cycle_Count++;
            }
        }
        else
        {
            beep_Count = 0;
            beep_Alarm_Count = 0;
            beep_Cycle = 0;
            beep_Cycle_Count = 0;
            Beep_Enable = 0;
        }

    }
}

